package com.moment.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.moment.model.InstrumentBankData;
import com.moment.model.InstrumentCardData;
import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;



@WebServlet("/InstrumentController")
public class InstrumentController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public InstrumentController() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doCAll(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doCAll(request, response);
	}

	protected void doCAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String instrumentfor = request.getParameter("instrumentfor");

		System.out.println("instrument for=" + instrumentfor);
	
		if (instrumentfor.equalsIgnoreCase("view")) {

		} else if (instrumentfor.equalsIgnoreCase("edit")) {
			String iname = request.getParameter("iname");
			String mobilenumber = (String) request.getSession().getAttribute(
					"username");
			//--New CODE-------------
			Map<String,String> map=new HashMap<String,String>();
			map.put("method", "23");
			map.put("mnum",mobilenumber);
			map.put("nickname",iname);

			String json=new Converter().mapToJson(map);
			System.out.println("json String :"+json);
			Services serviceproxy=new ServicesProxy();
			
			String instrumentType=serviceproxy.service(json);
//---------------------------
			
//			String instrumentType = proxy
//					.getInstrumentType(mobilenumber, iname);
			
			System.out.println("MobileNumber=" + mobilenumber
					+ " instrument name=" + iname);
			//--New CODE-------------
			Map<String,String> map1=new HashMap<String,String>();
			map1.put("method", "22");
		map1.put("mnum",mobilenumber);
					map1.put("nickname",iname);

		String json1=new Converter().mapToJson(map1);
			System.out.println("json String :"+json1);
			Services serviceproxy1=new ServicesProxy();
			
			String output=serviceproxy1.service(json1);
//---------------------------
			//String output = proxy.instrumentDetails(mobilenumber, iname);
			System.out.println("output" + output);
			JSONArray jArray;
			JSONObject jobj = null;
			HttpSession session = request.getSession();
			if (instrumentType.equalsIgnoreCase("card")) {
				InstrumentCardData instrumentCardData = new InstrumentCardData();

				try {
					jArray = new JSONArray(output);
					jobj = jArray.getJSONObject(0);
					instrumentCardData.setNickname(jobj.getString("NickName"));
					instrumentCardData.setCardno(jobj.getString("data1"));
					instrumentCardData.setDlimit(jobj.getString("dlimit"));
					instrumentCardData.setMlimit(jobj.getString("mlimit"));
					instrumentCardData.setExpirydate(jobj.getString("data2"));
					instrumentCardData.setInstrutype(jobj.getString("type"));
					instrumentCardData.setInstruId(jobj.getString("InstruId"));
					System.out.println("instrument id:"
							+ instrumentCardData.getInstruId());
					instrumentCardData.setCardtype(jobj.getString("data3"));
					session.setAttribute("instrumentObj", instrumentCardData);
					response.sendRedirect("/MMP/jsp/viewinstrument_card.jsp");

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {

				InstrumentBankData instrumentbankdata = new InstrumentBankData();

				try {
					jArray = new JSONArray(output);
					jobj = jArray.getJSONObject(0);
					instrumentbankdata.setNickname(jobj.getString("NickName"));
					instrumentbankdata.setBankname(jobj.getString("data3"));
					instrumentbankdata.setDlimit(jobj.getString("dlimit"));
					instrumentbankdata.setMlimit(jobj.getString("mlimit"));
					instrumentbankdata.setAccountno(jobj.getString("data2"));
					instrumentbankdata.setInstrutype(jobj.getString("type"));
					instrumentbankdata.setIntruId(jobj.getString("InstruId"));
					instrumentbankdata.setAccounttype(jobj.getString("data1"));
					session.setAttribute("instrumentObj", instrumentbankdata);
					response.sendRedirect("/MMP/jsp/viewinstrument.jsp");
					/*
					 * RequestDispatcher
					 * rd=getServletContext().getRequestDispatcher
					 * ("/jsp/viewinstrument.jsp");
					 * rd.include(request,response);
					 */
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}

}
